// pages/houseSearch2/houseSearch2.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    showCoverInp: true,
    submitData: {
      content: '',
      pageNumber: 10,
      currentPage: 1
    },
    houseList: [],
    hasData: true,
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.getAllHouse()
  },
  inputFocus: function (e) {
    this.setData({
      showCoverInp: false
    })
  },
  inputBlur: function (e) {
    if (this.data.commName == '') {
      this.setData({
        showCoverInp: true
      })
    }
  },
  changeInput: function (e) {
    this.setData({
      ['submitData.currentPage']: 1,
      houseList: [],
      commName: e.detail.value,
      ['submitData.content']: e.detail.value
    })
    this.getAllHouse()
  },
  // 获取房源所有的数据
  getAllHouse: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyHouseRental/home/search',
      data: this.data.submitData,
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if (res.data.success) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          var houses = res.data.data.pp;
          for (var i = 0; i < houses.length; i++) {
            if (houses[i].picture) {
              houses[i].picture = houses[i].picture.split(",");
            }
            if (houses[i].housingCharacteristics) {
              houses[i].housingCharacteristics = houses[i].housingCharacteristics.split(",");
            }
            _this.data.houseList.push(houses[i])
          }
          _this.setData({
            houseList: _this.data.houseList
          })
          if (_this.data.houseList.length == res.data.data.page.totalNumber) {
            _this.setData({
              hasData: false
            })
          }
        }
      }
    })
  },
  // 进入详情页
  enterDetail: function (e) {
    var flag = e.currentTarget.dataset.flag;
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/houseDetail/houseDetail?flag=' + flag + '&name=' + item.title + '&id=' + item.id + '&communityName=' + item.communityName
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      ['submitData.currentPage']: 1,
      houseList: []
    })
    this.getAllHouse();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      ['submitData.currentPage']: this.data.submitData.currentPage + 1
    })
    this.getAllHouse();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})